package com.qianji.petmall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianji.common.constant.WareConstant;
import com.qianji.common.utils.PageUtils;
import com.qianji.common.utils.Query;
import com.qianji.petmall.ware.dao.WareOrderTaskDao;
import com.qianji.petmall.ware.entity.WareOrderTaskEntity;
import com.qianji.petmall.ware.service.WareOrderTaskService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;


@Service("wareOrderTaskService")
public class WareOrderTaskServiceImpl extends ServiceImpl<WareOrderTaskDao, WareOrderTaskEntity> implements WareOrderTaskService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WareOrderTaskEntity> page = this.page(
                new Query<WareOrderTaskEntity>().getPage(params),
                new QueryWrapper<WareOrderTaskEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<WareOrderTaskEntity> getOrderStaticByOrderSn(String orderSn) {
        if (orderSn==null){
            return Collections.emptyList();
        }
        List<WareOrderTaskEntity> wareOrderTaskEntities = this.baseMapper.selectAllByOrderSnAndTaskStatus(orderSn, WareConstant.StockStatus.LOCKED.getCode());
        if (wareOrderTaskEntities==null){
            return Collections.emptyList();
        }
        return wareOrderTaskEntities;
    }

    @Override
    public PageUtils queryPageByOrder(Map<String, Object> params) {
        QueryWrapper<WareOrderTaskEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (StringUtils.isNotEmpty(key)){
            wrapper.eq("order_id",key)
                    .or().like("consignee",key);
        }
        IPage<WareOrderTaskEntity> page = this.page(new Query<WareOrderTaskEntity>().getPage(params), wrapper);
        return new PageUtils(page);
    }

}